package com.yujistu.manager;


import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Win on 2015/11/6.
 */
public class ObserverManger implements ObserverChanger {

    /******************************** 定义相关observer的token *************************************/
    public static final String LOCATION_CITY = "location_city";

    static HashMap<String, ObserverManger> mangerMap = new HashMap<String, ObserverManger>();

    private ObserverManger() {
        super();
    }

    public static ObserverManger getInstance(String token) {
        if (mangerMap.get(token) == null) {
            mangerMap.put(token, new ObserverManger());
        }
        return mangerMap.get(token);
    }

    private ArrayList<Observer> observers = new ArrayList<Observer>();


    @Override
    public void registerObserver(Observer o) {
        if (!observers.contains(o))
            observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        int i = observers.indexOf(o);
        if (i >= 0) {
            observers.remove(o);
        }
    }

    @Override
    public void notifyObserver(Object obj) {
       for(Observer observer : observers){
           observer.update(obj);
       }
    }
}
